package com.tanhua.server.service;

import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.UserLocation;
import com.tanhua.domain.vo.NearUserVo;
import com.tanhua.domain.vo.UserLocationVo;
import com.tanhua.dubbo.api.db.UserInfoApi;
import com.tanhua.dubbo.api.mongo.UserLocationApi;
import com.tanhua.server.interceptors.UserHolder;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BaiduService {

    @DubboReference
    private UserLocationApi userLocationApi;
    @DubboReference
    private UserInfoApi userInfoApi;

    public ResponseEntity saveLocation(Double longitude, Double latitude, String addrStr) {

        userLocationApi.save(UserHolder.getUserId(),longitude,  latitude,  addrStr); //dubbo方法


        return ResponseEntity.ok(null);

    }

    public ResponseEntity search(String gender, Integer distance) {
       List<UserLocationVo> userLocationList = userLocationApi.search(UserHolder.getUserId(),distance);

        List<NearUserVo> nearUserVoList = new ArrayList<>();
        for (UserLocationVo userLocation : userLocationList) {
            //            排除自己
            if(userLocation.getUserId().longValue()== UserHolder.getUserId().longValue()){
                continue;
            }

            UserInfo userInfo = userInfoApi.findById(userLocation.getUserId());
            //            处理性别
            if(StringUtils.equals(gender,userInfo.getGender())){
                NearUserVo nearUserVo = new NearUserVo();
                nearUserVo.setUserId(userInfo.getId());
                nearUserVo.setAvatar(userInfo.getAvatar());
                nearUserVo.setNickname(userInfo.getNickname());
                nearUserVoList.add(nearUserVo);
            }
        }
        return ResponseEntity.ok(nearUserVoList);


    }
}
